home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
SCROLL_B
/
SCROLL_B.C
Wrap
C/C++ Source or Header
|
1990-10-10
|
11KB
|
273 lines
/*
File: Scroll Bar.c
Author: Dana Basken
Date: October 10, 1990 (hey, the shuttle just landed!)
(text entered at Monaco 9, tabs are 4 spaces)
*/
/************************************************************************/
/* demo of text help routine. */
/************************************************************************/
ControlHandle myControl,hitControlHdl;
WindowPtr myWindow,hitWindow;
Rect myRect,textRect,scrollRect;
int programOver,hitControl,dummy,currentValue,minValue,maxValue,oldValue;
EventRecord myEvent;
Handle myText;
TEHandle myTE;
/************************************************************************/
/* initialize() */
/* */
/* standard initialization */
/* initialize help variables as well */
/* */
/************************************************************************/
void initialize() {
/************************************************************/
/* initialize everything as normal: */
/************************************************************/
MaxApplZone();
InitGraf(&thePort);
InitFonts();
FlushEvents(everyEvent, 0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(0L);
InitCursor();
/************************************************************/
/* get the help text from the resource file: */
/************************************************************/
myText = GetResource('TEXT',128);
MoveHHi(myText);
HLock(myText);
/************************************************************/
/* textRect is the rectangle that outlines the text area */
/* scrollRect is the actual text outline (1 pixel in from */
/* textRect */
/* myRect is the rectangle for the scroll bar */
/************************************************************/
SetRect(&textRect,27,10,314,190);
SetRect(&scrollRect,28,11,313,189);
SetRect(&myRect,10,10,26,190);
/************************************************************/
/* open up myWindow from resource file and set it to be the*/
/* current grafport. Set the font to be Monaco 9pt, frame */
/* the text area. */
/************************************************************/
myWindow = GetNewWindow(128,0L,-1L);
SetPort(myWindow);
TextFont(monaco);
TextSize(9);
FrameRect(&textRect);
/************************************************************/
/* open a TextEdit record for the help text and set the */
/* text in the record to be our help text resource. */
/************************************************************/
myTE = TENew(&scrollRect,&scrollRect);
TESetText((*myText),SizeResource(myText),myTE);
/************************************************************/
/* re-calculate the TextEdit record so we get an accurate */
/* count of the number of text lines inside our scrollRect */
/* rectangle. */
/************************************************************/
TECalText(myTE);
/************************************************************/
/* create the scroll bar control. set the current value to*/
/* be 1, the low value to be 1 and the high value should be*/
/* set to last line in the text edit record minus the */
/* number of lines that will fit in the rectangle (looks */
/* much nicer that way). */
/************************************************************/
myControl = NewControl(myWindow,&myRect,"\p",1,1,1,
(**myTE).nLines-((scrollRect.bottom-scrollRect.top)/(**myTE).lineHeight)+1,
scrollBarProc,0L);
}
/************************************************************************/
/* actionProc() */
/* */
/* handles the all actions from the TrackControl excluding the */
/* inThumb routine (handled later). */
/************************************************************************/
pascal void actionProc(actControl,actPart)
ControlHandle actControl;
int actPart; {
/************************************************************/
/* hold on to the minimum and maximum values for this */
/* control, as well as the current value (also saved as the*/
/* oldValue). */
/************************************************************/
minValue = GetCtlMin(actControl);
maxValue = GetCtlMax(actControl);
currentValue = GetCtlValue(actControl);
oldValue = currentValue;
/************************************************************/
/* which part of the scroll bar was clicked in? */
/************************************************************/
switch (actPart) {
/************************************************************/
/* was it the up-arrow button? */
/************************************************************/
case inUpButton:
/************************************************************/
/* if the current value is not already at the top, then: */
/************************************************************/
if (currentValue!=minValue) {
SetCtlValue(actControl,currentValue-1); /* move the control up 1 */
TEScroll(0,(**myTE).lineHeight,myTE); /* move the text down 1 line */
}
break;
/************************************************************/
/* was it the down-arrow button? */
/************************************************************/
case inDownButton:
/************************************************************/
/* if the current value is not already at the bottom, then:*/
/************************************************************/
if (currentValue!=maxValue) {
SetCtlValue(actControl,currentValue+1); /* move the control down 1 */
TEScroll(0,-(**myTE).lineHeight,myTE); /* move the text up 1 line */
}
break;
/************************************************************/
/* was it in the scroll region above the thumb? */
/************************************************************/
case inPageUp:
/************************************************************/
/* attempt to move the control up 1/10 of the total text */
/* if the control is too low, set it to the minimum value */
/* now, move the control. */
/* scroll the text by the offset of the old value-new */
/************************************************************/
currentValue -= ((maxValue-minValue)/10);
if (currentValue < minValue) {currentValue = minValue;}
SetCtlValue(actControl,currentValue);
TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
break;
/************************************************************/
/* was it in the scroll region below the thumb? */
/************************************************************/
case inPageDown:
/************************************************************/
/* attempt to move the control down 1/10 of the total text */
/* if the control is too high, set it to the maximum value */
/* now, move the control. */
/* scroll the text by the offset of the old value-new */
/************************************************************/
currentValue += ((maxValue-minValue)/10);
if (currentValue > maxValue) {currentValue = maxValue;}
SetCtlValue(actControl,currentValue);
TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
break;
}
}
/************************************************************************/
/* main() */
/* */
/* initializes program and handles events */
/* */
/************************************************************************/
void main() {
initialize();
programOver = 0;
do {
SystemTask();
/************************************************************/
/* use WaitNextEvent for MultiFinder, or switch it to */
/* GetNextEvent if you want. */
/************************************************************/
if (WaitNextEvent(everyEvent,&myEvent,15,0L)) {
switch (myEvent.what) {
case updateEvt:
BeginUpdate(myEvent.message);
DrawControls(myEvent.message); /* make sure control is visible */
EraseRect(&textRect);
TEUpdate(&scrollRect,myTE); /* update the help text */
FrameRect(&textRect); /* draw frame around text */
EndUpdate(myEvent.message);
break;
case activateEvt:
if ((myEvent.modifiers&activeFlag)!=0) {
HiliteControl(myControl,0); /* activate: control is on */
} else {
HiliteControl(myControl,255); /* deactiviate: control is off */
}
break;
case app4Evt:
if (myEvent.message & 1L) { /* resume event */
HiliteControl(myControl,0);
} else { /* suspend event */
HiliteControl(myControl,255);
}
break;
case keyDown:
case autoKey:
break; /* do nothing in this demo */
case mouseDown:
/************************************************************/
/* when the mouse is clicked, find out what it was clicked */
/* on. we handle the control only on a click in the */
/* content region: */
/************************************************************/
switch (FindWindow(myEvent.where,&hitWindow)) {
case inContent:
/************************************************************/
/* change the mouse coordinates to local, then see which */
/* part of the control (if any) was clicked on. if the */
/* mouse was clicked on the up or down buttons or in the */
/* scroll region, we'll use our action procedure defined */
/* above to handle it, otherwise (ie. the thumb was clicked*/
/* on) we handle it here. */
/************************************************************/
GlobalToLocal(&myEvent.where);
hitControl=FindControl(myEvent.where,myWindow,&hitControlHdl);
if (hitControlHdl == myControl) {
switch (hitControl) {
case inUpButton:
case inDownButton:
case inPageUp:
case inPageDown:
dummy = TrackControl(hitControlHdl,myEvent.where,&actionProc);
break;
case inThumb:
/************************************************************/
/* similar to in the action procedure above, this section */
/* will get the old value before calling TrackControl and */
/* the new value after TrackControl. The difference from */
/* old to current value is the number of lines to shift the*/
/* text. Note that if no movement of the thumb took place,*/
/* we don't move anything. */
/************************************************************/
oldValue = GetCtlValue(hitControlHdl);
dummy = TrackControl(hitControlHdl,myEvent.where,0L);
currentValue = GetCtlValue(hitControlHdl);
if (oldValue!=currentValue) {
TEScroll(0,(**myTE).lineHeight*(oldValue-currentValue),myTE);
}
break;
}
}
break;
case inGoAway:
if (TrackGoAway(hitWindow,myEvent.where)) {programOver=1;}
break;
case inDrag:
DragWindow(hitWindow,myEvent.where,&screenBits.bounds);
break;
}
break;
}
}
} while (programOver==0);
HUnlock(myText);
DisposHandle(myText);
DisposeWindow(myWindow);
}